package guowenlong.golf.ui.friendlymatch;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import guowenlong.golf.R;
import me.wenlong.mixutils.mvp.BaseActivity;

/**
 * des   : 赛道
 * author: Administrator
 * email : guowenlong20000@gmail.com
 * time  : 2017年04月27日 下午 11:48.
 */

public class TrackActivity extends BaseActivity implements OnTouchListener{

  @Override
  protected int getContentView(Bundle savedInstceState) {
    return R.layout.activity_track;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    DisplayMetrics dm = getResources().getDisplayMetrics();
    screenWidth = dm.widthPixels;
    screenHeight = dm.heightPixels - 50;

    mBaseView   = (RelativeLayout)findViewById(R.id.base_view);
    mTouchButton = (ImageView) findViewById(R.id.touch);
    mTouchButton.setOnTouchListener(this);
    mTextA = (TextView) findViewById(R.id.a);
    mTextB = (TextView) findViewById(R.id.b);

    //mBaseView.addView(new LineView(this, 527, 44, 527, 1614));
    lineA = new LineView(this, 400, LineView.TYPE_HOLE);
    lineB = new LineView(this, 400, LineView.TYPE_T);
    mBaseView.addView(lineA);
    mBaseView.addView(lineB);
  }

  @Override
  protected void initData() {

  }
  private ImageView mTouchButton;
  private int lastX, lastY;
  private int screenWidth, screenHeight;

  private TextView mTextA;
  private TextView mTextB;

  private LineView lineA;
  private LineView lineB;

  private RelativeLayout mBaseView;


  @Override
  protected void onResume() {
    super.onResume();

  }

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    int ea = event.getAction();

    lineA.setDistanceBetweenHoleAndT(getDistance(mTextA.getX(),mTextA.getY(),mTextB.getX(),mTextB.getY()));
    lineB.setDistanceBetweenHoleAndT(getDistance(mTextA.getX(),mTextA.getY(),mTextB.getX(),mTextB.getY()));

    Log.i("MainActivity", "mTextB height is " +mTextB.getLayout().getHeight() );
    lineA.drawLine(mTextA.getX(), mTextA.getY(), mTouchButton.getX(), mTouchButton.getY());
    lineB.drawLine(mTouchButton.getX(), mTouchButton.getY(), mTextB.getX(), mTextB.getY());

    switch (ea) {
      case MotionEvent.ACTION_DOWN:
        lastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标
        lastY = (int) event.getRawY();
        break;

      case MotionEvent.ACTION_MOVE:
        int dx = (int) event.getRawX() - lastX;
        int dy = (int) event.getRawY() - lastY;

        int l = v.getLeft() + dx;
        int b = v.getBottom() + dy;
        int r = v.getRight() + dx;
        int t = v.getTop() + dy;

        //下面判断移动是否超出屏幕
        if (l < 0) {
          l = 0;
          r = l + v.getWidth();
        }

        if (t < 0) {
          t = 0;
          b = t + v.getHeight();
        }

        if (r > screenWidth) {
          r = screenWidth;
          l = r - v.getWidth();
        }

        if (b > screenHeight) {
          b = screenHeight;
          t = b - v.getHeight();
        }
        v.layout(l, t, r, b);

        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
                /*Toast.makeText(this,
                        "当前位置："+l+","+t+","+r+","+b,
                        Toast.LENGTH_SHORT).show();*/
        v.postInvalidate();
        break;
      case MotionEvent.ACTION_UP:
        break;
    }
    return false;
  }

  private int getDistance(float startX, float startY, float endX, float endY){

    float xDist = endX - startX;
    float yDist = endY - startY;

    double distance = Math.sqrt((xDist * xDist) + (yDist * yDist));

    return (int)Math.round(distance);
  }
}
